package org.airline.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;

import org.airline.common.IModuleName;
import org.airline.entity.FlightArrange;
import org.airline.entity.OrderItem;
import org.airline.entity.PageBean;
import org.airline.service.FlightArrangeService;
import org.airline.service.OrderItemService;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

@Component("OrderItemAction")
@SuppressWarnings("all")
public class OrderItemAction extends SuperAction{

	private String forwardURL;
	private IModuleName iModuleName;
	private FlightArrange flightArrange;
	private OrderItem orderItem;
	private FlightArrangeService flightArrangeService;
	private OrderItemService orderItemService;
	private PageBean pageBean;
	private String keyWord;
	private int page = 1;
	private int pageSize = 5;
	private ArrayList shoppingCart; // 购物车
	private Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
	private int id;
	private int num = 1;
	
	// 添加到购物车
	public String addShoppingCartById() throws Exception {
		try {
			hm.put(flightArrange.getId(), num);
			System.out.println("hm ==" + hm);
			if (shoppingCart == null) {
				shoppingCart = new ArrayList();
			}
			/*flightArrange = flightArrangeService.getFlightArrangeById(flightArrange.getId());
			shoppingCart.add(flightArrange);*/
			shoppingCart.clear();
			Iterator it = hm.entrySet().iterator();
	        while (it.hasNext()) {
	        	
	            Map.Entry entry = (Map.Entry) it.next();
	            Object key = entry.getKey();
//	            if ((Integer)key == flightArrange.getId()) {
	            	flightArrange = flightArrangeService.getFlightArrangeById((Integer)key);
	            	shoppingCart.add(flightArrange);
//	            }
	        }
//	        ServletActionContext.getRequest().getSession().setAttribute("shoppingCart", null);
			ServletActionContext.getRequest().getSession().setAttribute("shoppingCart", shoppingCart);
		} catch (Exception e) {
			logFailureString(iModuleName.ADD, iModuleName.ADD, "添加到购物车失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.ADD, iModuleName.ADD, "添加到购物车成功");
		forwardURL = "OrderInfo!getOrderItemInfoByUserId";
		return "orderInfo";
	}

	// 根据ID取消预订机票
	public String removeShoppingCartById() throws Exception {
		try {
			hm.remove(flightArrange.getId());
			System.out.println("hm ===" + hm);
			Iterator it = hm.entrySet().iterator();
			shoppingCart.clear();
	        while (it.hasNext()) {
	            Map.Entry entry = (Map.Entry) it.next();
	            Object key = entry.getKey();
				flightArrange = flightArrangeService.getFlightArrangeById((Integer)key);
				shoppingCart.add(flightArrange);
	        }
			ServletActionContext.getRequest().getSession().setAttribute("shoppingCart", shoppingCart);
		} catch (Exception e) {
			e.printStackTrace();
		}
		logSuccessString(iModuleName.ADD, iModuleName.ADD, "删除商品成功");
		forwardURL = "/jsp/showMyCart.jsp";
		return SUCCESS;
	}
	// 清空购物车
	public String clearShoppingCart() throws Exception {
		try {
			if (shoppingCart != null) {
				shoppingCart.clear();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		logSuccessString(iModuleName.ADD, iModuleName.ADD, "清空购物车成功");
		forwardURL = "/jsp/showMyCart.jsp";
		return SUCCESS;
	}
	
	// 添加订单
	public String addOrderItemInfo() throws Exception {
		try {
			orderItemService.saveOrderItem(orderItem);
		} catch (Exception e) {
			logFailureString(iModuleName.ADD, iModuleName.ADD, "订单生成失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.ADD, iModuleName.ADD, "订单生成成功");
		forwardURL = "OrderInfo!getOrderItemInfoByUserId";
		return "orderInfo";
	}
	
	/*public String getOrderItemInfo() throws Exception{
		try {
			pageBean = orderItemService.queryForPage(pageSize, page);
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据页数获取订单失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据用户ID获取订单成功");
		forwardURL = "/jsp/showMyCart.jsp";
		return SUCCESS;
	}*/
	
	
	// 前台根据用户ID获取订单信息
	public String getOrderItemInfoByUserId() throws Exception {
		try {
			pageBean = orderItemService.queryForPageByUserId(id, pageSize, page);
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据用户ID获取订单失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据用户ID获取订单成功");
		forwardURL = "/jsp/showMyCart.jsp";
		return SUCCESS;
	}
	
	// 前台取消订单
	public String deleteOrderItemInfo() throws Exception {
		try {
			orderItemService.deleteOrderItem(orderItem.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.DELETE, iModuleName.DELETE, "取消订单失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.DELETE, iModuleName.DELETE, "取消订单成功");
		forwardURL = "OrderInfo!getOrderItemInfoByUserId";
		return "orderInfo";
	}
	// 后台删除订单
	public String deleteOrderItem() throws Exception {
		try {
			orderItemService.deleteOrderItem(orderItem.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.DELETE, iModuleName.DELETE, "取消订单失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.DELETE, iModuleName.DELETE, "取消订单成功");
		forwardURL = "OrderInfo!getAllOrderItemInfo";
		return "orderInfo";
	}
	
	//后台根据ID获取订单信息
	public String getOrderItemById() throws Exception {
		try {
			orderItem = orderItemService.getorderItemById(orderItem.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据ID获取订单信息失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据ID获取订单信息成功");
		forwardURL = "/admin/updateOrderItem.jsp";
		return SUCCESS;
	}
	
	//后台根据关键字分页查询订单信息
	public String getOrderItermsByKeyWords() throws Exception {
		try {
			pageBean = orderItemService.queryForPageByKeyWords(orderItem.getIsPayed(), pageSize,
					page);
		} catch (Exception e) {
			super.logFailureString(iModuleName.GET, iModuleName.GET,
					"根据关键字分页查询订单信息失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.GET, iModuleName.GET,
				"根据关键字分页查询订单信息成功");
		forwardURL = "/admin/showBackMyCart.jsp";
		return SUCCESS;
	}
	
	// 后台修改订单
	public String updateOrderItemInfo() throws Exception {
		try {
			orderItemService.updateOrderItem(orderItem);
			} catch (Exception e) {
				logFailureString(iModuleName.GET, iModuleName.GET, "修改订单信息失败", e);
				e.printStackTrace();
			}
			logSuccessString(iModuleName.GET, iModuleName.GET, "修改订单信息成功");
			forwardURL = "OrderInfo!getAllOrderItemInfo";
			return "orderInfo";
		}
	
	// 后台获取所有订单
	public String getAllOrderItemInfo() throws Exception {
		try {
			pageBean = orderItemService.queryForPage(pageSize, page);
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "获取所有订单失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "获取所有订单成功");
		forwardURL = "/admin/showBackMyCart.jsp";
		return SUCCESS;
	}
	
	public String getForwardURL() {
		return forwardURL;
	}
	public void setForwardURL(String forwardURL) {
		this.forwardURL = forwardURL;
	}
	public IModuleName getiModuleName() {
		return iModuleName;
	}
	public void setiModuleName(IModuleName iModuleName) {
		this.iModuleName = iModuleName;
	}
	public FlightArrange getFlightArrange() {
		return flightArrange;
	}
	public void setFlightArrange(FlightArrange flightArrange) {
		this.flightArrange = flightArrange;
	}
	public OrderItem getOrderItem() {
		return orderItem;
	}
	public void setOrderItem(OrderItem orderItem) {
		this.orderItem = orderItem;
	}
	public FlightArrangeService getFlightArrangeService() {
		return flightArrangeService;
	}
	
	@Resource(name = "FlightArrangeService")
	public void setFlightArrangeService(FlightArrangeService flightArrangeService) {
		this.flightArrangeService = flightArrangeService;
	}
	public OrderItemService getOrderItemService() {
		return orderItemService;
	}
	
	@Resource(name = "OrderItemService")
	public void setOrderItemService(OrderItemService orderItemService) {
		this.orderItemService = orderItemService;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public ArrayList getShoppingCart() {
		return shoppingCart;
	}

	public void setShoppingCart(ArrayList shoppingCart) {
		this.shoppingCart = shoppingCart;
	}

	public Map<Integer, Integer> getHm() {
		return hm;
	}

	public void setHm(Map<Integer, Integer> hm) {
		this.hm = hm;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}
	
	
}
